package com.probox.common.entity.service.customer.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.entity.domain.customer.CustomerTaskInfoRecord;
import com.probox.common.entity.mapper.customer.CustomerLevelInfoMapper;
import com.probox.common.entity.mapper.customer.CustomerTaskInfoRecordMapper;
import com.probox.common.entity.mapper.product.ProductInfoMapper;
import com.probox.common.entity.service.customer.ICustomerTaskInfoRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * 会员完成任务列Service业务层处理
 *
 * @author usoft
 * @date 2022-09-30
 */
@Service
@Transactional
public class CustomerTaskInfoRecordServiceImpl extends ServiceImpl<CustomerTaskInfoRecordMapper, CustomerTaskInfoRecord> implements ICustomerTaskInfoRecordService {
    @Autowired
    private CustomerTaskInfoRecordMapper customerTaskInfoRecordMapper;

    @Autowired
    private CustomerLevelInfoMapper customerLevelInfoMapper;



    @Autowired
    private ProductInfoMapper productInfoMapper;

    /**
     * 查询会员完成任务列
     *
     * @param recordId 会员完成任务列ID
     * @return 会员完成任务列
     */
    @Override
    public CustomerTaskInfoRecord selectCustomerTaskInfoRecordById(Long recordId) {
        return this.getById(recordId);
    }

    /**
     * 查询会员完成任务列列表
     *
     * @param customerTaskInfoRecord 会员完成任务列
     * @return 会员完成任务列
     */
    @Override
    public List<CustomerTaskInfoRecord> queryList(CustomerTaskInfoRecord customerTaskInfoRecord) {
        QueryWrapper<CustomerTaskInfoRecord> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(customerTaskInfoRecord.getRecordId())) && customerTaskInfoRecord.getRecordId() != null) {
            queryWrapper.eq("record_id", customerTaskInfoRecord.getRecordId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerTaskInfoRecord.getTaskId())) && customerTaskInfoRecord.getTaskId() != null) {
            queryWrapper.eq("task_id", customerTaskInfoRecord.getTaskId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerTaskInfoRecord.getCustomerUid())) && customerTaskInfoRecord.getCustomerUid() != null) {
            queryWrapper.eq("customer_uid", customerTaskInfoRecord.getCustomerUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerTaskInfoRecord.getTaskCode())) && customerTaskInfoRecord.getTaskCode() != null) {
            queryWrapper.eq("task_code", customerTaskInfoRecord.getTaskCode());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerTaskInfoRecord.getTaskName())) && customerTaskInfoRecord.getTaskName() != null) {
            queryWrapper.eq("task_name", customerTaskInfoRecord.getTaskName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerTaskInfoRecord.getTaskDesc())) && customerTaskInfoRecord.getTaskDesc() != null) {
            queryWrapper.eq("task_desc", customerTaskInfoRecord.getTaskDesc());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerTaskInfoRecord.getTaskIcon())) && customerTaskInfoRecord.getTaskIcon() != null) {
            queryWrapper.eq("task_icon", customerTaskInfoRecord.getTaskIcon());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerTaskInfoRecord.getStartDate())) && customerTaskInfoRecord.getStartDate() != null) {
            queryWrapper.eq("start_date", customerTaskInfoRecord.getStartDate());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerTaskInfoRecord.getEndDate())) && customerTaskInfoRecord.getEndDate() != null) {
            queryWrapper.eq("end_date", customerTaskInfoRecord.getEndDate());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerTaskInfoRecord.getTaskBrief())) && customerTaskInfoRecord.getTaskBrief() != null) {
            queryWrapper.eq("task_brief", customerTaskInfoRecord.getTaskBrief());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerTaskInfoRecord.getIsShow())) && customerTaskInfoRecord.getIsShow() != null) {
            queryWrapper.eq("is_show", customerTaskInfoRecord.getIsShow());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerTaskInfoRecord.getScore())) && customerTaskInfoRecord.getScore() != null) {
            queryWrapper.eq("score", customerTaskInfoRecord.getScore());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerTaskInfoRecord.getExpValue())) && customerTaskInfoRecord.getExpValue() != null) {
            queryWrapper.eq("exp_value", customerTaskInfoRecord.getExpValue());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerTaskInfoRecord.getServiceItemId())) && customerTaskInfoRecord.getServiceItemId() != null) {
            queryWrapper.eq("service_item_id", customerTaskInfoRecord.getServiceItemId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerTaskInfoRecord.getCardId())) && customerTaskInfoRecord.getCardId() != null) {
            queryWrapper.eq("card_id", customerTaskInfoRecord.getCardId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerTaskInfoRecord.getCouponId())) && customerTaskInfoRecord.getCouponId() != null) {
            queryWrapper.eq("coupon_id", customerTaskInfoRecord.getCouponId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerTaskInfoRecord.getProductId())) && customerTaskInfoRecord.getProductId() != null) {
            queryWrapper.eq("product_id", customerTaskInfoRecord.getProductId());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerTaskInfoRecord.getTaskSort())) && customerTaskInfoRecord.getTaskSort() != null) {
            queryWrapper.eq("task_sort", customerTaskInfoRecord.getTaskSort());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerTaskInfoRecord.getDataStatus())) && customerTaskInfoRecord.getDataStatus() != null) {
            queryWrapper.eq("data_status", customerTaskInfoRecord.getDataStatus());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerTaskInfoRecord.getCreateUser())) && customerTaskInfoRecord.getCreateUser() != null) {
            queryWrapper.eq("create_user", customerTaskInfoRecord.getCreateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerTaskInfoRecord.getCreateName())) && customerTaskInfoRecord.getCreateName() != null) {
            queryWrapper.eq("create_name", customerTaskInfoRecord.getCreateName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerTaskInfoRecord.getUpdateUser())) && customerTaskInfoRecord.getUpdateUser() != null) {
            queryWrapper.eq("update_user", customerTaskInfoRecord.getUpdateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(customerTaskInfoRecord.getUpdateName())) && customerTaskInfoRecord.getUpdateName() != null) {
            queryWrapper.eq("update_name", customerTaskInfoRecord.getUpdateName());
        }
        return customerTaskInfoRecordMapper.selectList(queryWrapper);
    }

    /**
     * 新增会员完成任务列
     *
     * @param customerTaskInfoRecord 会员完成任务列
     * @return 结果
     */
    @Override
    public boolean insertCustomerTaskInfoRecord(CustomerTaskInfoRecord customerTaskInfoRecord) {

        return this.save(customerTaskInfoRecord);

    }

    /**
     * 修改会员完成任务列
     *
     * @param customerTaskInfoRecord 会员完成任务列
     * @return 结果
     */
    @Override
    public boolean updateCustomerTaskInfoRecord(CustomerTaskInfoRecord customerTaskInfoRecord) {

        return this.updateById(customerTaskInfoRecord);
    }

    /**
     * 批量删除会员完成任务列
     *
     * @param recordIds 需要删除的会员完成任务列ID
     * @return 结果
     */
    @Override
    public boolean deleteCustomerTaskInfoRecordByIds(Long[] recordIds) {
        return this.removeByIds(Arrays.asList(recordIds));
    }

    @Override
    public List<CustomerTaskInfoRecord> queryListA(CustomerTaskInfoRecord customerTaskInfoRecord) {
        List<CustomerTaskInfoRecord> list=customerTaskInfoRecordMapper.selectRecord(customerTaskInfoRecord) ;
        CustomerTaskInfoRecord vo=null;
        Iterator<CustomerTaskInfoRecord> it=list.iterator();
        while (it.hasNext()){
            vo=it.next();
          vo.setLevelName(customerLevelInfoMapper.getLevelNameByUid(vo.getCustomerUid()))  ;

            if (ObjectUtils.isNotEmpty(vo.getProductId()))
                vo.setProducName(productInfoMapper.selectById(vo.getProductId()).getProductName());
        }
        return list ;
    }

    @Override
    public List<CustomerTaskInfoRecord> queryCustomerTask(String code, Long customerId) {
        QueryWrapper<CustomerTaskInfoRecord> queryWrapper = Wrappers.query();
        queryWrapper.eq("customer_uid", customerId);
        queryWrapper.eq("task_code", code);
        return customerTaskInfoRecordMapper.selectList(queryWrapper);
    }


}
